/**
 * Performed when page is loaded
 */
var log02010Response;
document.addEventListener('DOMContentLoaded', function() {
	init();
}, false);

function init() {
	var startYear;
	var endYear;
	var toDay;
	var fromDay;
	var options;
	toDay = new Date();
	fromDay = new Date();
	fromDay.setDate(toDay.getDate() - 14);
	endYear = toDay.getFullYear();
	startYear = endYear - 10;
	// get old search parameters
	if (document.getElementById("pFromYear").value
			&& document.getElementById("pFromMonth").value
			&& document.getElementById("pFromDay").value) {
		fromDay = new Date(document.getElementById("pFromYear").value, document
				.getElementById("pFromMonth").value - 1, document
				.getElementById("pFromDay").value);
	}
	if (document.getElementById("pToYear").value
			&& document.getElementById("pToMonth").value
			&& document.getElementById("pToDay").value) {
		toDay = new Date(document.getElementById("pToYear").value, document
				.getElementById("pToMonth").value - 1, document
				.getElementById("pToDay").value);
	}
	
	options = new Object();
	options.startYear = startYear;
	options.endYear = endYear;
	options.selectedYear = fromDay.getFullYear();
	options.selectedMonth = fromDay.getMonth() + 1;
	options.selectedDay = fromDay.getDate();
	options.firstText = "All";
	options.firstValue = "";
	initDateSelect("fromYear", "fromMonth", "fromDay", options);
	
	options = new Object();
	options.startYear = startYear;
	options.endYear = endYear;
	options.selectedYear = toDay.getFullYear();
	options.selectedMonth = toDay.getMonth() + 1;
	options.selectedDay = toDay.getDate();
	options.firstText = "All";
	options.firstValue = "";
	initDateSelect("toYear", "toMonth", "toDay", options);
	initialCall();
	document.getElementById("btnApprove").onclick = submitApprove;
	document.getElementById("btnReject").onclick = submitReject;
	document.getElementById("btnSearch").onclick = submitSearch;
//	document.getElementById("fromYear").onchange = submitSearch;
//	document.getElementById("fromMonth").onchange = submitSearch;
//	document.getElementById("fromDay").onchange = submitSearch;
//	document.getElementById("toYear").onchange = submitSearch;
//	document.getElementById("toMonth").onchange = submitSearch;
//	document.getElementById("toDay").onchange = submitSearch;
//	document.getElementById("proId").onchange = submitSearch;
//	document.getElementById("empId").onchange = submitSearch;
//	document.getElementById("status").onchange = submitSearch;
}

function validate() {
    var fromYear = document.getElementById("fromYear").value;
    var fromMonth = document.getElementById("fromMonth").value;
    var fromDay = document.getElementById("fromDay").value;
    var toYear = document.getElementById("toYear").value;
    var toMonth = document.getElementById("toMonth").value;
    var toDay = document.getElementById("toDay").value;
    var today = new Date();
    var errMes = "";
    if (fromYear && fromMonth && fromDay && toYear && toMonth && toDay) {
    	fromYear = parseInt(fromYear);
    	fromMonth = parseInt(fromMonth);
    	fromDay = parseInt(fromDay);
    	toYear = parseInt(toYear);
    	toMonth = parseInt(toMonth);
    	toDay = parseInt(toDay);
        if (fromYear < toYear
            || (fromYear == toYear && (fromMonth < toMonth || (fromMonth == toMonth && fromDay <= toDay)))) {
        } else {
            errMes += ("From Date must be before To Date\n");
        }
        if (/*(fromYear < today.getFullYear()
            || (fromYear == today.getFullYear() && (fromMonth < today.getMonth() + 1 || (fromMonth == today.getMonth() + 1 && fromDay <= today.getDate())))) &&*/ (toYear < today.getFullYear()
            || (toYear == today.getFullYear() && (toMonth < today.getMonth() + 1 || (toMonth == today.getMonth() + 1 && toDay <= today.getDate()))))) {

        } else {
            errMes += ("Please choose a date in the past\n[DEBUG] ty:" + (toYear == today.getFullYear()) + "; tm:" + (toMonth == today.getMonth() + 1) + "; td:" + (toDay <= today.getDate()));
        }
    }
    if (errMes) {
        alert(errMes);
        return false;
    }
    return true;
}

function dateFromString(s) {
	var tokens = s.split(/[\+\/:-]/);
	var d = new Date(tokens[0], tokens[1] - 1, tokens[2]);

	return d;
}

function populateSelect(options) {
	var selectId = options.selectId;
	var xmlDoc = options.xmlDoc;
	var root = options.root;
	var field = options.field;
	var selectElem;
	var value;
	var text;
	var selectedValue;
	var selected;
	if (selectId && xmlDoc && root && (field || field == 0)) {
		selectElem = document.getElementById(selectId);
		selectedValue = cleanSelect(selectElem);
		if (options.selectedValue) {
			selectedValue = options.selectedValue;
		}
		if (options.firstText) {
			if (options.firstValue != null) {
				selectElem.options[selectElem.options.length] = new Option(
						options.firstText, options.firstValue, true, false);
			} else {
				selectElem.options[selectElem.options.length] = new Option(
						options.firstText, options.firstText, true, false);
			}
		}
		var children = xmlDoc.getElementsByTagName(root)[0].childNodes;
		for ( var i = 0; i < children.length; i++) {
			var child = children[i];
			text = child.childNodes[field].textContent;
			if (options.useId) {
				value = child.attributes.getNamedItem("id").value;
			} else {
				value = text;
			}
			if (selectedValue && selectedValue == value) {
				selected = true;
			} else {
				selected = false;
			}
			selectElem.options[selectElem.options.length] = new Option(text,
					value, false, selected);
		}
	}
}

function cleanSelect(selectElem) {
	if (selectElem) {
		if (selectElem.options[selectElem.selectedIndex]) {
			var selectedVal = selectElem.options[selectElem.selectedIndex].value;
			selectElem.options.length = 0;
			return selectedVal;
		} else {
			selectElem.options.length = 0;
		}
	}
	return null;
}

function submitApprove(event) {
	var form = document.getElementById("form");
	form.setAttribute("action", "approve");
	form.submit();
	return false;
}

function submitReject(event) {
	var form = document.getElementById("form");
	form.setAttribute("action", "reject");
	form.submit();
	return false;
}

function initialCall(event) {
	var xmlHttp;
	var target = document.getElementById("btnSearch");
	var url;
	var options;
	target.disabled = true;
	try {
		url = "listApprove?search=all";
//				+ "&fromYear=" + document.getElementById("fromYear").value
//				+ "&fromMonth=" + document.getElementById("fromMonth").value 
//				+ "&fromDay=" + document.getElementById("fromDay").value 
//				+ "&toYear="+ document.getElementById("toYear").value 
//				+ "&toMonth=" + document.getElementById("toMonth").value 
//				+ "&toDay=" + document.getElementById("toDay").value
//				+ "&proId=" + document.getElementById("proId").value
//				+ "&empId=" + document.getElementById("empId").value
//				+ "&status=" + document.getElementById("status").value;
		if (window.XMLHttpRequest) {
			// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlHttp = new XMLHttpRequest();
		} else {
			// code for IE6, IE5
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				log02010Response = xmlHttp.responseXML;

				options = new Object();
				options.selectId = "proId";
				options.root = "projects";
				options.field = 0;
				options.xmlDoc = log02010Response;
				options.firstText = "All";
				options.firstValue = "";
				options.selectedValue = document.getElementById("pProId").value;
				populateSelect(options);

				options = new Object();
				options.selectId = "empId";
				options.root = "employees";
				options.field = 0;
				options.xmlDoc = log02010Response;
				options.firstText = "All";
				options.firstValue = "";
				options.selectedValue = document.getElementById("pEmpId").value;
				populateSelect(options);
				
				selectStatus(document.getElementById("pStatus").value);
				submitSearch(event);

//				options = new Object();
//				options.tableId = "actList";
//				options.root = "activities";
//				options.firstColumnCheckBox = true;
//				options.xmlDoc = log02010Response;
//				options.colOptions = new Array();
//				options.colOptions[0] = new Object();
//				options.colOptions[0].type = "date";
//				populateTable(options);
				target.disabled = false;
			}
		};
		xmlHttp.open("POST", url, true);
		xmlHttp.send();
	} catch (e) {
		alert(e);
		target.disabled = false;
	}
	return false;
}

function selectStatus(status) {
	if (status) {
		var statusElem = document.getElementById("status");
		var options;
		var option;
		var size;
		options = statusElem.options;
		size = options.length;
		for (var i = 0; i < size; i++) {
			option = options[i];
			if (option.value == status) {
				option.selected = true;
				break;
			}
		}
	}
}

function submitSearch(event) {
//	var xmlHttp;
//	var url;
	var options;
	if (validate()) {
		try {
//			url = "listApprove?search=activities"
//					+ "&fromYear=" + document.getElementById("fromYear").value 
//					+ "&fromMonth=" + document.getElementById("fromMonth").value 
//					+ "&fromDay=" + document.getElementById("fromDay").value 
//					+ "&toYear="+ document.getElementById("toYear").value 
//					+ "&toMonth=" + document.getElementById("toMonth").value 
//					+ "&toDay=" + document.getElementById("toDay").value
//					+ "&proId=" + document.getElementById("proId").value
//					+ "&empId=" + document.getElementById("empId").value
//					+ "&status=" + document.getElementById("status").value;
//			if (window.XMLHttpRequest) {
//				// code for IE7+, Firefox, Chrome, Opera, Safari
//				xmlHttp = new XMLHttpRequest();
//			} else {
//				// code for IE6, IE5
//				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
//			}
//			xmlHttp.onreadystatechange = function() {
//				if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
//					log02010Response = xmlHttp.responseXML;
					options = new Object();
					options.tableId = "actList";
					options.root = "activities";
					options.firstColumnCheckBox = true;
					options.xmlDoc = log02010Response;
					options.colOptions = new Array();
					options.colOptions[0] = new Object();
					options.colOptions[0].type = "date";
					options.criteria = getCriteria();
					populateTable(options);
//				}
//			};
//			xmlHttp.open("POST", url, true);
//			xmlHttp.send();
		} catch (e) {
			alert(e);
		}
	}
//	return true;
	return false;
}

function getCriteria() {
	var criteria = new Array();
	var criterion;
	var value;
	// from Date
	value = getDate("fromDay", "fromMonth", "fromYear");
	if (value) {
		criterion = new Object();
		criterion.field = 0;
		criterion.operator = "ge";
		criterion.value = value;
		criterion.type = "date";
		criteria.push(criterion);
	}
	// to Date
	value = getDate("toDay", "toMonth", "toYear");
	if (value) {
		criterion = new Object();
		criterion.field = 0;
		criterion.operator = "le";
		criterion.value = value;
		criterion.type = "date";
		criteria.push(criterion);
	}
	// project ID
	value = document.getElementsByName("proId")[0].value;
	if (value && value != 0) {
		criterion = new Object();
		criterion.field = 3;
		criterion.operator = "eq";
		criterion.value = value;
		criterion.type = "string";
		criteria.push(criterion);
	}
	// employee ID
	value = document.getElementsByName("empId")[0].value;
	if (value && value != 0) {
		criterion = new Object();
		criterion.field = 2;
		criterion.operator = "eq";
		criterion.value = value;
		criterion.type = "string";
		criteria.push(criterion);
	}
	// status
	value = document.getElementsByName("status")[0].value;
	if (value) {
		criterion = new Object();
		criterion.field = 5;
		criterion.operator = "eq";
		criterion.value = value;
		criterion.type = "string";
		criteria.push(criterion);
	}
	return criteria;
}

function getDate(dayId, monthId, yearId) {
	var dayElem = document.getElementById(dayId);
	var monElem = document.getElementById(monthId);
	var yearElem = document.getElementById(yearId);
	var date = null;
	if (dayElem && dayElem.value && monElem && monElem.value && yearElem
			&& yearElem.value) {
		date = new Date(yearElem.value, monElem.value - 1, dayElem.value);
	}
	return date;
}